package cn.wolfcode.trip.base.service.impl;

import cn.wolfcode.trip.base.domain.StrategyComment;
import cn.wolfcode.trip.base.domain.TravelComment;
import cn.wolfcode.trip.base.mapper.StrategyCommentMapper;
import cn.wolfcode.trip.base.mapper.TravelCommentMapper;
import cn.wolfcode.trip.base.query.QueryObject;
import cn.wolfcode.trip.base.service.IStrategyCommentService;
import cn.wolfcode.trip.base.service.ITravelCommentService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class TravelCommentServiceImpl implements ITravelCommentService {
    @Autowired
    private TravelCommentMapper mapper;


    @Override
    public PageInfo<TravelComment> selectList(QueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize(), qo.getOrderBy());
        List<TravelComment> list = mapper.selectForList(qo);
        return new PageInfo<>(list);
    }


    @Override
    public void saveOrUpdate(TravelComment travelComment) {

        Date date = new Date();

        if (travelComment.getId() == null) {
            travelComment.setCreateTime(date);
            mapper.insert(travelComment);


        } else {
            mapper.updateByPrimaryKey(travelComment);
        }
    }

    @Override
    public TravelComment selectById(Long id) {
        return mapper.selectByPrimaryKey(id);

    }

    @Override
    public int CommentMeNumById(Long id) {
        int num = mapper.selectCommentMeNumById(id);
        return num;
    }

    @Override
    public PageInfo<TravelComment> listCommentMesInfo(QueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize(), qo.getOrderBy());
        List<TravelComment> list = mapper.selectCommentMesInfo(qo);
        return new PageInfo<>(list);
    }

    @Override
    public void changeIsReadById(Long id, boolean isRead) {
        List<Long> travelId = mapper.selectCommentMesTravel(id);
        if(travelId.size()>0){
            mapper.updateIsReadById(travelId,isRead);
        }
    }
}
